*************************************************************************
*																		*
*					Campaign Panel Book Chapter 6 - Choice variable		*
*																		*
*************************************************************************	

*******************************************************************************
** Generate an alternative operationalization, other parties in reference category
*******************************************************************************
** I.1 Generate vote intention variable
*******************************************************************************   

fre kp1_190bb

// Pre-election vote intention
foreach welle of numlist 1/6 {
   recode kp`welle'_190bb (-98=98) (-99=.) (-97/-1=.) (215=8) (322=9) ///
   (149/209=10) (225 801=10), copyrest gen(wabsa`welle')
}

// ( 10 = other party)

tab kp1_190bb wabsa1, m

// Post-election voting behaviour
recode kp7_200bb (-99/-1=.) (149/209=10) (215=8) (322=9) (225 801=10), ///
   copyrest gen(wabsa7)

 // Postal voters: Postal vote is added to vote intention variable
 // For waves after postal vote the value from postal vote is imputed


//2013
foreach welle of numlist 3/6 {	
replace wabsa`welle' = kp`welle'_191bb if kp`welle'_170==6
recode wabsa`welle' (149/209=10) (215=8) (322=9) (225 801=10) (-98 -93 -97 -99=.)
}


replace wabsa7 = kp7_191bb if kp7_180==6
recode wabsa7 (149/209=10) (215=8) (322=9) (225 801=10) (-98 -99=.)

foreach welle of numlist 4/7 {
replace wabsa`welle' = wabsa3 if kp3_170==6
}

foreach welle of numlist 5/7 {
replace wabsa`welle' = wabsa4 if kp4_170==6
}

foreach welle of numlist 6/7 {
replace wabsa`welle' = wabsa5 if kp5_170==6
}

foreach welle of numlist 7/7 {
replace wabsa`welle' = wabsa6 if kp6_170==6
}
   
  

*******************************************************************************
** I.2 Create binary vote intention dummies (undecided voters are "missings")
*******************************************************************************

foreach welle of numlist 1/7 {
   foreach partei of numlist 1 4/9 {
      gen parteia`partei'`welle' = wabsa`welle'==`partei' if wabsa`welle'!=98 & ///
	     wabsa`welle'!=.
   }
}  

*******************************************************************************
** I.3 Create binary vote intention dummies (undecided voters are taken into account
* as a separate category)
*******************************************************************************

foreach welle of numlist 1/7 {
   foreach partei of numlist 1 4/9 {
      gen parta`partei'`welle' = wabsa`welle'==`partei' if  ///
	     wabsa`welle'!=.
   }
}  //Respondent who did not want to vote in one wave counts as "missing" in this wave
	
tab wabsa4 parta44, m

// Part0 is a dummy indicating indecision
foreach welle of numlist 1/7 {
   gen parta0`welle' = wabsa`welle'==98 if wabsa`welle'!=.
}

tab1 parteia55 parta44



*******************************************************************************
** I.4 Generate variable which indicates whether a repondent possesses a stable
** voting intention thoughout the campaing panel
*******************************************************************************

foreach welle of numlist 1/7 {
   recode wabsa`welle' (8 9 = 98), gen(wabsha`welle') copyrest
}


egen sdwabsa = rowsd(wabsa?)
gen stablea = 0 if sdwabsa!=.
replace stablea = 1 if sdwabsa==0
drop wabsha*




**************************************************************************
** Generate variable indicating whether a person has a stable voting	**
** intention for a specific party   									**
**************************************************************************

/* Background: We analyze party vote intention separately for different parties
over time. It is possible that a respondent is unstable over time regarding a 
particular party (e.g., switching between SPD and Greens) but stable regarding other
parties (e.g., will never vote CDU or FDP). Only those respondents are included 
in the party models which show variation regarding a particular party.*/


egen sdwabscdu = rowsd(parta1?)
egen sdwabsspd = rowsd(parta4?)
egen sdwabsfdp = rowsd(parta5?)
egen sdwabsgru = rowsd(parta6?)
egen sdwabslink = rowsd(parta7?)

gen stablecdu = 0 if sdwabscdu!=.
replace stablecdu = 1 if sdwabscdu==0

gen stablespd = 0 if sdwabsspd!=.
replace stablespd = 1 if sdwabsspd==0

gen stablefdp = 0 if sdwabsfdp!=.
replace stablefdp = 1 if sdwabsfdp==0

gen stablegru = 0 if sdwabsgru!=.
replace stablegru = 1 if sdwabsgru==0

gen stablelink = 0 if sdwabslink!=.
replace stablelink = 1 if sdwabslink==0

fre stable*  //4588 cases with minimum two valid observations

lab define stable ///
	0 "Variation" ///
	1 "immer stabil"
lab val stable* stable

/* Among parties the share of stables fluctuates between 77% and 94% (FDP)*/


*-----------------------------------------------
* Case selection variables
*--------------------------------------------------

* Never ever non-voters
gen neverno = 1 
replace neverno = 0 if kp1_170>3 & kp1_170<=5 ///
	| kp2_170>3 & kp2_170<=5 | kp3_170>3 & kp3_170<=5| ///
	kp4_170>3 & kp4_170<=5 | kp5_170>3 & kp5_170<=5 | ///
	kp6_170>3 & kp6_170<=5 | kp7_180==2

list kp?_170 neverno if kp4_170==6, nol	

lab define neverno ///
	0 "phasenweise Nichtwähler" ///
	1 "niemals Nichtwahlabsichten"
lab val neverno neverno
fre neverno //15% indicate not wanting to turn out at least once

* 7 valid observations in vote intention?
gen allvalid = 0
replace allvalid = 1 if !missing(wabsa1, wabsa2, wabsa3, wabsa4, ///
	wabsa5, wabsa6, wabsa7)

fre allvalid  //nur 56%
fre allvalid if kp4_170==6

list wabsa? allvalid

* valid observations in vote intention?
egen help = rowmiss(wabsa?)

gen twovalid = 0
replace twovalid = 1 if help<6

drop help

* Number of cases for party models

cd "${path}\Reproducible Do-Files\Chapter 6"

estimates clear
foreach var of varlist stable* {
eststo: estpost tab `var' 
}

esttab using Fallauswahl.rtf, cells ("b pct(fmt(0))") noobs ///
	title("Fallauswahl: Mind. zwei gültige Beobachtungen Wahlabsicht." ///
	"Befragte mit stabilen und nichtstabilen Wahlabsichten") ///
	mtitle("Allgemein" "CDU/CSU" "SPD" "FDP" "Grüne" "Linke") compress	///
	replace
	
estimates clear	
foreach var of varlist stable* {
eststo: estpost tab `var' if neverno==1
}

esttab using Fallauswahl.rtf, cells ("b pct(fmt(0))") noobs ///
	title("Fallauswahl: Mind. zwei gültige Beobachtungen Wahlabsicht + zu keinem Zeitpunkt Nichtwahlabsicht." ///
	"Befragte mit stabilen und nichtstabilen Wahlabsichten") ///
	mtitle("Allgemein" "CDU/CSU" "SPD" "FDP" "Grüne" "Linke") compress	///
	append

estimates clear	
foreach var of varlist stable* {
eststo: estpost tab `var' if n_teilnahme==7
}

esttab using Fallauswahl.rtf, cells ("b pct(fmt(0))") noobs ///
	title("Fallauswahl: Teilnahme an allen 7 Wellen." ///
	"Befragte mit stabilen und nichtstabilen Wahlabsichten") ///
	mtitle("Allgemein" "CDU/CSU" "SPD" "FDP" "Grüne" "Linke") compress	///
	append

estimates clear
foreach var of varlist stable* {
eststo: estpost tab `var' if neverno==1 & n_teilnahme==7
}

esttab using Fallauswahl.rtf, cells ("b pct(fmt(0))") noobs ///
	title("Fallauswahl: Teilnahme an allen 7 Wellen + zu keinem Zeitpunkt Nichtwähler." ///
	"Befragte mit stabilen und nichtstabilen Wahlabsichten") ///
	mtitle("Allgemein" "CDU/CSU" "SPD" "FDP" "Grüne" "Linke") compress	///
	append


estimates clear
foreach var of varlist stable* {
eststo: estpost tab `var' if allvalid==1
}

esttab using Fallauswahl.rtf, cells ("b pct(fmt(0))") noobs ///
	title("Fallauswahl: 7 gültige Beobachtungen Wahlabsicht." ///
	"Befragte mit stabilen und nichtstabilen Wahlabsichten") ///
	mtitle("Allgemein" "CDU/CSU" "SPD" "FDP" "Grüne" "Linke") compress	///
	append



fre stable* if neverno==1 & allvalid==1


* How many votes does the SPD receive?
egen countspd = rowtotal(parta4?), missing


* For instable voters: in precisely which waves is the vote intention for a 
* particular party observed?

egen spdpattern = concat(parta4?) if allvalid==1 

estimates clear
foreach num of numlist 0/7 {
estpost tab spdpattern if countspd==`num' & allvalid==1
estimates store num`num'
}

foreach num of numlist 0/7 {
esttab num`num' using "Pattern SPD-Wahlabsichten.rtf", cells("b pct(fmt(0))") noobs ///
	title("`num' mal Absicht für SPD:") ///
	nonumber append
}

estimates clear

*------------------------------------------------------------------------
* Lazarsfeld-Types without non-voters (new coding for Crystalizers
*-------------------------------------------------------------------------

* Transform variable vote intention into correct format

* Recode necessary in order to be able to copy code from Do-File: "Wahlabsicht Kreuzungen"
* other parties besides the five established ones should have the same code
* "do not know" = 888
* Non-voters are already "missing", postal voters are integrated

foreach num of numlist 1/7{
gen wabsrestr_novote`num' = wabsa`num'
recode wabsrestr_novote`num' (8/10=801) (98=888)
}

tab wabsa4 wabsrestr_novote4, m 

* target manifestations
label define lazar3 ///
	1 "Partisan" ///
	2 "Crystalizer" ///
	3 "Waverer" ///
	4 "Party Changer"

	
gen lazarstyp_novote_cry=.

*** Partisans (for all parties, including stable others)
replace lazarstyp_novote_cry=1 if wabsrestr_novote1==wabsrestr_novote2 & wabsrestr_novote2==wabsrestr_novote3 ///
	& wabsrestr_novote3==wabsrestr_novote4 & wabsrestr_novote4==wabsrestr_novote5 ///
	& wabsrestr_novote5==wabsrestr_novote6  & wabsrestr_novote6==wabsrestr_novote7 ///
	& wabsrestr_novote7<=801

*** Crystalizer (A vote intention can only crystallize for undecided voters. This vote intention does not necessarily have to remain stable)
replace lazarstyp_novote_cry=2 if wabsrestr_novote1==888 & wabsrestr_novote2==888 & wabsrestr_novote3==888 & wabsrestr_novote4==888 ///
	& wabsrestr_novote5==888  & wabsrestr_novote6==888 & wabsrestr_novote7<888
replace lazarstyp_novote_cry=2 if wabsrestr_novote1==888 & wabsrestr_novote2==888 & wabsrestr_novote3==888 & wabsrestr_novote4==888 ///
	& wabsrestr_novote5==888  & wabsrestr_novote6<888   & wabsrestr_novote7<888
replace lazarstyp_novote_cry=2 if wabsrestr_novote1==888 & wabsrestr_novote2==888 & wabsrestr_novote3==888 & wabsrestr_novote4==888 ///
	& wabsrestr_novote5<888 & wabsrestr_novote6<888  & wabsrestr_novote7<888
replace lazarstyp_novote_cry=2 if wabsrestr_novote1==888 & wabsrestr_novote2==888 & wabsrestr_novote3==888 & wabsrestr_novote4<888  ///
	& wabsrestr_novote5<888 & wabsrestr_novote6<888  & wabsrestr_novote7<888
replace lazarstyp_novote_cry=2 if wabsrestr_novote1==888 & wabsrestr_novote2==888 & wabsrestr_novote3<888 & wabsrestr_novote4<888 ///
	& wabsrestr_novote5<888 & wabsrestr_novote6<888   & wabsrestr_novote7<888
replace lazarstyp_novote_cry=2 if wabsrestr_novote1==888 & wabsrestr_novote2<888 & wabsrestr_novote3<888 & wabsrestr_novote4<888  ///
	& wabsrestr_novote5<888 & wabsrestr_novote6<888   & wabsrestr_novote7<888

***Waverer (Respondents indicate the same party in wave 1 and wave 7 but make different statements in some intermediate waves)
replace lazarstyp_novote_cry=3 if wabsrestr_novote1==wabsrestr_novote7 & ///
	(wabsrestr_novote1!=wabsrestr_novote2 | wabsrestr_novote1!=wabsrestr_novote3 | wabsrestr_novote1!=wabsrestr_novote4 | ///
	wabsrestr_novote1!=wabsrestr_novote5 | wabsrestr_novote1!=wabsrestr_novote6) & wabsrestr_novote7<888

***Party Changer (Respondents who change their vote intention between wave 1 and wave 7)
replace lazarstyp_novote_cry=4 if wabsrestr_novote1<888 & wabsrestr_novote7<888 & wabsrestr_novote1!=wabsrestr_novote7

***Unsure Crystalizer
replace lazarstyp_novote_cry=2 if lazarstyp_novote_cry==. & allvalid==1

list wabsrestr_novote?  lazarstyp_novote_cry if allvalid==0

replace lazarstyp_novote_cry=. if allvalid==0


list wabsrestr_novote* lazars* if allvalid==1


lab val laz lazar3
fre laz if n_teilnahme==7




*---------------------------------
* Save
*----------------------------------


keep lfdn wabsa* parta* stable* spd* count* neverno allvalid twovalid lazars*

save "${path}\Reproducible Do-Files\Chapter 6\Data\choice13.dta", replace
   
